package com.jinsp.cameralibrary.camera;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.jinsp.cameralibrary.PermissionFragment;
import com.jinsp.cameralibrary.R;

// TODO: 1/5/22 外部使用CameraFragment自行处理权限失败问题
public class CameraActivity extends AppCompatActivity implements PermissionFragment.PermissionErrorCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        setContentView(R.layout.activity_camera);
        //获取配置参数
        CameraConfigBean cameraConfigBean = getIntent().getParcelableExtra(CameraConfigBean.TAG);
        if (cameraConfigBean == null) {
            finish();
        } else {
            //配置标题栏
            Toolbar toolbar = findViewById(R.id.tool_bar_camera);
            if (cameraConfigBean.isShowTitleBar()) {
                toolbar.setTitle("");
                setSupportActionBar(toolbar);
                toolbar.setBackgroundColor(cameraConfigBean.getTitleBackgroundColor());
                TextView titleTextView = toolbar.findViewById(R.id.tv_camera_title);
                titleTextView.setText(cameraConfigBean.getTitleText());
                titleTextView.setTextColor(cameraConfigBean.getTitleTextColor());
                ImageButton backImageButton = toolbar.findViewById(R.id.ib_camera_back);
                if (cameraConfigBean.getBackDrawableId() != 0) {
                    backImageButton.setImageDrawable(getResources().getDrawable(cameraConfigBean.getBackDrawableId()));
                    backImageButton.setOnClickListener(v -> finish());
                } else {
                    backImageButton.setVisibility(View.GONE);
                }
            } else {
                toolbar.setVisibility(View.GONE);
                //全屏显示
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
            }
            CameraFragment cameraFragment = CameraFragment.newInstance(cameraConfigBean);
            cameraFragment.registerPermissionCallback(this);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_camera, cameraFragment, CameraFragment.TAG)
                    .commit();
        }
    }

    @Override
    public void permissionGrantDenied(String deniedPermission) {
        AlertDialog permissionDialog = new AlertDialog.Builder(this)
                .setMessage("权限被禁用，需要进入设置-->应用-->权限 手动开启权限")
                .setPositiveButton("设置", (dialog, which) -> {
                    dialog.dismiss();
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setData(Uri.fromParts("package", getPackageName(), null));
                    startActivity(intent);
                })
                .create();
        permissionDialog.show();
    }

    @Override
    public void permissionGrantFailed(String failedPermission) {
        Toast.makeText(this, "权限不足", Toast.LENGTH_SHORT).show();
    }
}